# Создание нового проекта
django-admin startproject django_course # django_academind_book_store

# Создание нового приложения
python manage.py startapp challenges

# Запуск сервера
python manage.py runserver

# Форматирование кода
# на mac option + command + L
# на windows ctrl + alt + I

# Миграции
python manage.py makemigrations
python manage.py migrate


# терминал Django
python manage.py shell
from book_outlet.models import Book

# Inserting Data
harry_potter = Book(title="Harry Potter 1 - The Philosopher's Stone", rating=5)
harry_potter.save()
lord_pf_the_rings = Book(title="Lord of the Rings", rating=4)
lord_pf_the_rings.save()

# Getting Data
Book.objects.all()

# Update Data
python manage.py makemigrations  # select 2 Добавить в models null=True
python manage.py migrate
python manage.py shell
from book_outlet.models import Book
lotr = Book.objects.all()[1].author
harry_potter = Book.objects.all()[0]
harry_potter.title = "Harry Potter 1"
harry_potter.author = "J.K. Rowling"
harry_potter.is_bestselling = True
harry_potter.save()
Book.objects.all()[0].author
lotr = Book.objects.all()[1]
lotr.author = "J.R.R. Tolkien"
lotr.is_bestselling = True
lotr.save()

# Delete Data
harry_potter = Book.objects.all()[0]
harry_potter.delete()
Book.objects.all()

# Create Instead of Save
Book.objects.create(title="Harry Potter 1 - The Philosopher's Stone", rating=5, author="J.K. Rowling", is_bestselling=True)
Book.objects.create(title="Some random book", rating=1, author="Random Dude", is_bestselling=False)
Book.objects.all()

# Querying & Filtering Data
Book.objects.get(id=3)
Book.objects.get(rating=5)  # если несколько книг, то будет ошибка. только один элемент должен быть
Book.objects.filter(is_bestselling=True) # будет несколько элементов
Book.objects.filter(rating__lt=3)  # rating < 3
Book.objects.filter(rating__lt=3, title__contains="story")
Book.objects.filter(rating__lt=3, title__icontains="story")

# "or" Conditions
from django.db.models import Q
Book.objects.filter(Q(rating__lt=3) | Q(is_bestselling=True))
Book.objects.filter(Q(rating__lt=3) | Q(is_bestselling=True), Q(author="J.K. Rowling"))

# Query Performance
bestsellers = Book.objects.filter(is_bestselling=True)
amazing_bestsellers = bestsellers.filter(rating__gt=4)  # rating > 4
print(amazing_bestsellers)

# Выход
exit()

# Slug
python manage.py shell
from book_outlet.models import Book
Book.objects.all()
Book.objects.get(title="Harry Potter 1").save()
Book.objects.get(title="Harry Potter 1").slug
Book.objects.get(title="Lord of the Rings").save()
Book.objects.get(title="Lord of the Rings").slug
Book.objects.get(title="Some random book").save()
Book.objects.get(title="Some random book").slug
python manage.py runserver


=== Admin ===
python manage.py createsuperuser
python manage.py runserver


# Если внесли ForeignKey в models
python manage.py shell
from book_outlet.models import Book
Book.objects.all().delete()
python manage.py migrate

# Working with Relations in Python Code
python manage.py shell
from book_outlet.models import Book, Author
jkrowling = Author(first_name="J.K.", last_name="Rowling")
jkrowling.save()
Author.objects.all()[0].first_name
hp1 = Book(title="Harry Potter 1", rating=5, is_bestselling=True, slug="harry-potter-1", author=jkrowling)
hp1.save()
Book.objects.all()
harrypotter = Book.objects.get(title="Harry Potter 1")
harrypotter.author
harrypotter.author.first_name
# Cross Model Queries
books_by_rowling = Book.objects.filter(author__last_name="Rowling")
books_by_rowling = Book.objects.filter(author__last_name__contains="wling")
jkr = Author.objects.get(first_name="J.K.")
jkr.book_set.all()
python manage.py makemigrations
python manage.py migrate
python manage.py shell
from book_outlet.models import Book, Author
jkr = Author.objects.get(first_name="J.K.")
jkr.books.all()
jkr.books.get(title="Harry Potter 1")
jkr.books.filter(rating__gt=3)

# One-to-one Python Code
python manage.py shell
from book_outlet.models import Book, Author, Address
Author.objects.all()[0].address
addr1 = Address(street="Some Street", postal_code="123456", city="London")
addr2 = Address(street="Another Street", postal_code="678900", city="New York")
addr1.save()
addr2.save()
jkr = Author.objects.get(first_name="J.K.")
jkr.address = addr1
jkr.save()

# Many-to-many Python Code
python manage.py shell
from book_outlet.models import Book, Country
germany = Country(name="Germany", code="DE")
germany.save()
mys = Book.objects.all()[1]
mys.published_countries.add(germany)
mys.published_countries.filter(code="DE")
ger = Country.objects.all()[0]
ger.book_set.all()








